//
//  TieZiTableViewCell.m
//  Liuhe
//
//  Created by zr on 2018/8/8.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "TieZiTableViewCell.h"

@interface TieZiTableViewCell ()

@property (nonatomic, weak)UIImageView * iconImageView;
@property (nonatomic, weak)UILabel * nameLabel;
@property (nonatomic, weak)UILabel * timeLabel;
@property (nonatomic, weak)UILabel * contentLabel;
@property (nonatomic, weak)UIImageView * backImageView;
@property (nonatomic, weak)UIImageView * sayImageView;
@property (nonatomic, weak)UILabel * sayLabel;
@property (nonatomic, weak)UIView * lineView;

@end

@implementation TieZiTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}


- (void)setModel:(ClubModel *)model{
    
    _model = model;
  
    NSDictionary * dict = model.user;
    
    self.nameLabel.text = dict[@"username"];
    NSURL * iconUrl = [NSURL URLWithString:dict[@"headimg"]];
    [self.iconImageView sd_setImageWithURL:iconUrl placeholderImage:ImageNamed(@"23sdasas")];
    
    // timeStampString 是服务器返回的13位时间戳
    NSString *timeStampString  = model.ctime;
    
    // iOS 生成的时间戳是10位
    NSTimeInterval interval    =[timeStampString doubleValue];
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString       = [formatter stringFromDate: date];
    //    NSLog(@"服务器返回的时间戳对应的时间是:%@",dateString);
    
    self.timeLabel.text = dateString;
    self.contentLabel.text = model.title;
    
    NSString * backStr = model.images.firstObject;
    NSURL * backUrl = [NSURL URLWithString:backStr];
    [self.backImageView sd_setImageWithURL:backUrl];
    
    self.sayLabel.text = [NSString stringWithFormat:@"%@",model.comments_count];
}

- (void)setMyModel:(MyTieziModel *)myModel{
    
    _myModel = myModel;
    
    NSDictionary * dict = myModel.user;
    
    self.nameLabel.text = dict[@"username"];
    NSURL * iconUrl = [NSURL URLWithString:dict[@"headimg"]];
    [self.iconImageView sd_setImageWithURL:iconUrl placeholderImage:ImageNamed(@"23sdasas")];
    
    // timeStampString 是服务器返回的13位时间戳
    NSString *timeStampString  = myModel.ctime;
    
    // iOS 生成的时间戳是10位
    NSTimeInterval interval    =[timeStampString doubleValue];
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString       = [formatter stringFromDate: date];
    //    NSLog(@"服务器返回的时间戳对应的时间是:%@",dateString);
    
    self.timeLabel.text = dateString;
    self.contentLabel.text = myModel.title;
    
    NSString * backStr = myModel.images.firstObject;
    NSURL * backUrl = [NSURL URLWithString:backStr];
    [self.backImageView sd_setImageWithURL:backUrl];
    
    self.sayLabel.text = [NSString stringWithFormat:@"%@",myModel.comments_count];
}


- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = WHITE_COLOR;
    
    UIImageView * iconImageView = [UIImageView new];
    
    //  把图片设置成圆形
    iconImageView.layer.cornerRadius = 18;//裁成圆角
    iconImageView.layer.masksToBounds = YES;//隐藏裁剪掉的部分
    iconImageView.backgroundColor = [UIColor grayColor];
    //    iconImageView.image = ImageNamed(@"头像7");
    
    self.iconImageView = iconImageView;
    
    [self.contentView addSubview:self.iconImageView];
    
    
    UILabel * nameLabel = [UILabel new];
    
    nameLabel.textColor = BLACK_COLOR;
    nameLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    nameLabel.text = @"阿斯蒂芬哪怕";
    
    self.nameLabel = nameLabel;
    
    [self.contentView addSubview:self.nameLabel];
    
    
    UILabel * timeLabel = [UILabel new];
    
    timeLabel.textColor = [UIColor lightGrayColor];
    timeLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    timeLabel.text = @"2分钟前";
    
    self.timeLabel = timeLabel;
    
    [self.contentView addSubview:self.timeLabel];
    
    
    UILabel * contentLabel = [UILabel new];
    
    contentLabel.textColor = BLACK_COLOR;
    contentLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    contentLabel.text = @"2desired background color to the backgroundView";
    
    self.contentLabel = contentLabel;
    
    [self.contentView addSubview:self.contentLabel];
    
    
    UIImageView * backImageView = [UIImageView new];
    
    backImageView.image = ImageNamed(@"");
    backImageView.backgroundColor = [UIColor lightGrayColor];
    
    self.backImageView = backImageView;
    
    [self.contentView addSubview:self.backImageView];

    
    UIImageView * sayImageView = [UIImageView new];
    
    sayImageView.image = ImageNamed(@"pl222");
    sayImageView.backgroundColor = WHITE_COLOR;
    
    self.sayImageView = sayImageView;
    
    [self.contentView addSubview:self.sayImageView];
    
    
    UILabel * sayLabel = [UILabel new];
    
    sayLabel.textColor = [UIColor lightGrayColor];
    sayLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    sayLabel.text = @"100";
    
    self.sayLabel = sayLabel;
    
    [self.contentView addSubview:self.sayLabel];
    
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xe6e6e6);
    
    self.lineView = lineView;
    
    [self.contentView addSubview:self.lineView];
}


- (void)layoutSubviews{
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentView).offset(15 * SCREENH6);
        make.top.mas_equalTo(self.contentView).offset(15 * SCREENH6);
        make.height.width.mas_equalTo(36 * SCREENH6);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.iconImageView.mas_trailing).offset(15 * SCREENH6);
        make.top.mas_equalTo(self.iconImageView);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.nameLabel);
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.iconImageView);
        make.trailing.mas_equalTo(self.contentView).offset(-15 * SCREENH6);
        make.top.mas_equalTo(self.iconImageView.mas_bottom).offset(15 * SCREENH6);
    }];
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.contentLabel);
        make.top.mas_equalTo(self.contentLabel.mas_bottom).offset(10 * SCREENH6);
        make.height.mas_equalTo(195 * SCREENH6);
    }];
    
    [self.sayImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backImageView);
        make.top.mas_equalTo(self.backImageView.mas_bottom).offset(13 * SCREENH6);
        make.height.mas_equalTo(14 * SCREENH6);
        make.width.mas_equalTo(16.5 * SCREENH6);
    }];
    
    [self.sayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.sayImageView.mas_trailing).offset(3 * SCREENH6);
        make.centerY.mas_equalTo(self.sayImageView);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.contentView);
        make.height.mas_equalTo(1 * SCREENH6);
        make.bottom.mas_equalTo(self.contentView);
    }];
    
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
